package com.dobby.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.OrderDto;
import com.dobby.pea.dto.PurchaseDto;
import com.dobby.pea.entity.*;
import com.dobby.pea.mapper.PurchaseMapper;
import com.dobby.pea.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Dobby
 * @version 1.0
 */
@Service
@Slf4j
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, tbPurchase> implements PurchaseService {

    @Autowired
    private PurchaseBidService purchaseBidService;
    @Autowired
    private UserService userService;

    @Autowired
    private TypeSecondService typeSecondService;



    @Transactional
    public R<String> deleteWithBid(Long id) {

        //修改采购表的isdeleted数据为1
        UpdateWrapper<tbPurchase> tbPurchaseUpdateWrapper = new UpdateWrapper<>();
        tbPurchaseUpdateWrapper.eq("id",id);
        tbPurchaseUpdateWrapper.set("is_deleted",1);
        this.update(tbPurchaseUpdateWrapper);

        //修改采购竞标的isdeleted数据为1
        UpdateWrapper<tbPurchaseBid> tbPurchaseBidUpdateWrapper = new UpdateWrapper<>();
        tbPurchaseBidUpdateWrapper.eq("purchase_id",id);
        List<tbPurchaseBid> list = purchaseBidService.list(tbPurchaseBidUpdateWrapper);

        if (list!=null){
            tbPurchaseBidUpdateWrapper.set("is_deleted",1);
            purchaseBidService.update(tbPurchaseBidUpdateWrapper);
        }

        return R.success("删除成功");
    }

    @Transactional
    public R<Page> pageAll(int page, int pageSize, String name) {
        //构造分页构造器对象
        Page<tbPurchase> PurchasePage = new Page<>(page,pageSize);
        Page<PurchaseDto> purchaseDtoPage = new Page<>();

        LambdaQueryWrapper<tbPurchase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),tbPurchase::getName,name);
        queryWrapper.orderByDesc(tbPurchase::getUpdateTime);

        //执行分页查询
        this.page(PurchasePage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(PurchasePage,purchaseDtoPage,"records");

        List<tbPurchase> records = PurchasePage.getRecords();
        List<PurchaseDto> list = records.stream().map((item) -> {
            PurchaseDto purchaseDto = new PurchaseDto();

            BeanUtils.copyProperties(item,purchaseDto);

            Long purchaseId  = item.getId();
            Long userId = item.getUserId();
            Long typeId = item.getTypeId();
            //根据id查询用户对象
            tbUser user = userService.getById(userId);
            tbTypeSecond tbTypeSecond = typeSecondService.getById(typeId);

            //根据id查询订单细节对象
            LambdaQueryWrapper<tbPurchaseBid> tbPurchaseBidLambdaQueryWrapper= new LambdaQueryWrapper<>();
            tbPurchaseBidLambdaQueryWrapper.eq(tbPurchaseBid::getIsDeleted,0);
            tbPurchaseBidLambdaQueryWrapper.eq(tbPurchaseBid::getPurchaseId,purchaseId);
            List<tbPurchaseBid> tbPurchaseBidList = purchaseBidService.list(tbPurchaseBidLambdaQueryWrapper);

            if(tbPurchaseBidList!=null) purchaseDto.setPurchaseBids(tbPurchaseBidList);


            purchaseDto.setUser(user);
            purchaseDto.setTypeSecond(tbTypeSecond);

            return purchaseDto;
        }).collect(Collectors.toList());

        purchaseDtoPage.setRecords(list);

        return R.success(purchaseDtoPage);
    }

    @Transactional
    public R<Page> pageUser(int page, int pageSize, String name, Long id) {
        //构造分页构造器对象
        Page<tbPurchase> PurchasePage = new Page<>(page,pageSize);
        Page<PurchaseDto> purchaseDtoPage = new Page<>();

        LambdaQueryWrapper<tbPurchase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tbPurchase::getUserId,id);
        List<tbPurchase> tbPurchases = this.list(queryWrapper);

        if (tbPurchases==null||tbPurchases.size()==0) return R.error("您未发起采购");


        queryWrapper.like(StringUtils.isNotEmpty(name),tbPurchase::getName,name);
        queryWrapper.orderByDesc(tbPurchase::getUpdateTime);

        //执行分页查询
        this.page(PurchasePage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(PurchasePage,purchaseDtoPage,"records");

        List<tbPurchase> records = PurchasePage.getRecords();
        List<PurchaseDto> list = records.stream().map((item) -> {
            PurchaseDto purchaseDto = new PurchaseDto();

            BeanUtils.copyProperties(item,purchaseDto);

            Long purchaseId  = item.getId();
            Long userId = item.getUserId();
            Long typeId = item.getTypeId();
            //根据id查询用户对象
            tbUser user = userService.getById(userId);
            tbTypeSecond tbTypeSecond = typeSecondService.getById(typeId);

            //根据id查询订单细节对象
            LambdaQueryWrapper<tbPurchaseBid> tbPurchaseBidLambdaQueryWrapper= new LambdaQueryWrapper<>();
            tbPurchaseBidLambdaQueryWrapper.eq(tbPurchaseBid::getIsDeleted,0);
            tbPurchaseBidLambdaQueryWrapper.eq(tbPurchaseBid::getPurchaseId,purchaseId);
            List<tbPurchaseBid> tbPurchaseBidList = purchaseBidService.list(tbPurchaseBidLambdaQueryWrapper);

            purchaseDto.setUser(user);
            purchaseDto.setPurchaseBids(tbPurchaseBidList);
            purchaseDto.setTypeSecond(tbTypeSecond);

            return purchaseDto;
        }).collect(Collectors.toList());

        purchaseDtoPage.setRecords(list);

        return R.success(purchaseDtoPage);
    }

    @Transactional
    public R<PurchaseDto> purchaseDtoById(Long id){
        log.info("id---------------->{}",id);
        PurchaseDto purchaseDto = new PurchaseDto();

        tbPurchase purchase = this.getById(id);

        log.info("初始生成------->{}",purchase);

        LambdaQueryWrapper<tbPurchaseBid> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(tbPurchaseBid::getPurchaseId,id);
        List<tbPurchaseBid> list = purchaseBidService.list(queryWrapper);


        BeanUtils.copyProperties(purchase,purchaseDto);

        tbUser user = userService.getById(purchase.getUserId());

        tbTypeSecond tbTypeSecond = typeSecondService.getById(purchase.getTypeId());

        purchaseDto.setUser(user);
        purchaseDto.setPurchaseBids(list);
        purchaseDto.setTypeSecond(tbTypeSecond);

        return R.success(purchaseDto);
    }
}
